let router = require('../router/Router');
let cmService = require('../service/cmService');
let agentService = require('../service/AgentService');
let userService = require('../service/userService');
let payAcctService = require('../service/payAcctService');
let qrCodeService = require('../service/QrCodeService');

const { BusiError, SysError } = require('../exception/exception');


/**
 * 本周业绩查询
 *
 */
router.post("/auth/perf/curweek", (req, resp) => {
    cmService.perfQuery(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

router.post('/auth/direct/perf/query', (req, resp) => {
    let cond = {
        uid: req.uid,
        page: req.body.page ? req.body.page : 1,
        cAcctId: req.body.cAcctId ? req.body.cAcctId : "",
        registerTime: req.body.registerTime ? req.body.registerTime : ""  
    };

    cmService.directPerfQuery(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

router.post('/auth/direct/query', (req, resp) => {
    let cond = {
        uid: req.uid,
        page: req.body.page ? req.body.page : 1,
        cAcctId: req.body.cAcctId ? req.body.cAcctId : "",
        registerTime: req.body.registerTime ? req.body.registerTime : ""  
    };

    cmService.directQuery(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 代理统计首页
 */
router.post("/auth/direct/stat", (req, resp) => {
    cmService.directStat(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 代理统计首页
 */
router.post("/auth/home/stat", (req, resp) => {
    cmService.homeStat(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 本周直属下属查询
 * page
 */
router.post("/auth/perf/curweek/du", (req, resp) => {
    if (!req.body.page) {
        req.body.page = 1;
    }
    let page = parseInt(req.body.page);
    if (page <= 0) page = 1;

    cmService.duPerfQuery(req.uid, page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 收益查询
 */
router.post("/auth/prof", (req, resp) => {
    cmService.profQuery(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("prof query err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 获取二维码
 */
router.get("/qrcode", (req, resp) => {
    try{
        resp.writeHead(200, {'Content-Type': 'image/png'});
        qrCodeService.getQrCodeStream(req.query.text).pipe(resp);
    }catch(e){
        // resp.writeHead(414, {'Content-Type': 'text/html'});
        // resp.end('<h1>414 Request-URI Too Large</h1>');
    }
});

/**
 * 收益提现查询
 *  @param Aid       *int64 `form:"aid"`
 AcctId    string `form:"acct_id"`
 TypeCode  string `form:"type_code"`
 FlowNo    string `form:"flow_no"`
 Status    *int64 `form:"status" validate:"exists"`
 StartTime string `form:"start_time"`
 EndTime   string `form:"end_time"`
 Page      *int64 `form:"page" validate:"exists"`
 */
router.post("/auth/prof/withdraw/query", (req, resp) => {
    if (!req.body.page) {
        req.body.page = 1;
    }
    let page = parseInt(req.body.page);
    if (page <= 0) page = 1;
    let cond = {
        page: page,
        acctId: req.uid,
        uid: req.uid
    };

    if (req.body.status != null && req.body.status !== undefined) {
        cond.status = parseInt(req.body.status);
    }

    if (req.body.flowNo) {
        cond.flowNo = req.body.flowNo;
    }

    if (req.body.startTime) {
        cond.startTime = req.body.startTime;
    }

    if (req.body.endTime) {
        cond.endTime = req.body.endTime;
    }

    if (req.body.cardNo){
        cond.cardNo = req.body.cardNo;
    }

    cmService.wdQuery(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("withdraw query err:", err);
        resp.send({code: 500, msg: err.message});
    });
});
